<template>
  <el-breadcrumb separator="/">
    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>生产管理</el-breadcrumb-item>
    <el-breadcrumb-item>工艺路线</el-breadcrumb-item>
  </el-breadcrumb>
  <el-row style="margin-top: 10px">
    <el-col :span="25">
      <el-form :model="craftQuery" inline>
        <el-form-item label="工艺路线编号">

          <el-input  v-model="craftQuery.craftId"/>
        </el-form-item>
        <el-form-item label="工艺路线名称">
          <el-input v-model="craftQuery.craftName"/>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-select placeholder="全部" style="width:100px" v-model="craftQuery.craftStatus">
            <el-option :value="0" label="未启用"/>
            <el-option :value="1" label="启用"/>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button @click="loadCraft">搜索</el-button>
          <el-button @click="back">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>

  <!-- 工具栏 -->
  <el-row style="margin-top: 10px">
    <el-col :span="24">
      <el-button type="primary" plain @click="toAdd"><el-icon><Plus /></el-icon>新增</el-button>
      <el-button type="success" plain @click="allPublish"><el-icon><EditPen /></el-icon>修改</el-button>
      <el-button type="danger" plain @click="allDelete"><el-icon><Delete /></el-icon>删除</el-button>
      <el-button type="warning" plain @click="allPigeonhole"><el-icon><Download /></el-icon>导出</el-button>
    </el-col>
  </el-row>

  <!-- 数据表 -->
  <el-row style="margin-top: 10px">
    <el-col :span="24">
      <el-table
          :data="craftList"
          border
          :stripe="true"
          ref="processTable"
      >
        <el-table-column type="selection" width="55" />

        <el-table-column prop="craftId" label="工艺路线编号" >
          <template #default="scope">
            <el-link type="primary" @click="toDetail(scope.row.craftRelId)">{{ scope.row.craftId }}</el-link>
          </template>
        </el-table-column>


        <el-table-column prop="craftName" label="工艺路线名"/>
        <el-table-column prop="craftExplain" label="工艺路说明"/>

        <el-table-column prop="craftStatus" label="是否启用">
          <template #default="scope">
            <el-tag type="info" v-if="scope.row.craftStatus == 0">未启用</el-tag>
            <el-tag type="primary" v-else-if="scope.row.craftStatus == 1">启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="craftRemark" label="备注"/>

        <el-table-column label="操作" width="330">
          <template #default="scope">
            <el-button-group>
              <template v-if="scope.row.userId == curId">
                <el-button size="small" type="primary" @click="toPrimaryCraft(scope.row.craftRelId)">修改</el-button>
                <el-popconfirm title="你确定要删除吗" @confirm="deleteCraft(scope.row.craftRelId)" >
                  <template #reference>
                    <el-button size="small" type="danger">删除</el-button>
                  </template>
                </el-popconfirm>
              </template>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>
  <el-row style="margin: 0px 10px;padding: 0px 15px">
    <el-col :span="24">
      <el-pagination @change="changePage" background layout="prev, pager, next" :total="total"/>
    </el-col>
  </el-row>


  <!-- 新增工序弹窗 -->
  <el-dialog
      v-model="craftAddWinDialogVisible"
      title="新增工序"
      width="500"
  >
    <el-form v-model="addCraft" label-width="auto">
      <el-form-item  label="编号">
        <el-input v-model="addCraft.craftId" />
        <el-switch v-model="value1"  active-text="自动生产" :change="getRandom()"/>
      </el-form-item>
      <el-form-item  label="名称">
        <el-input  v-model="addCraft.craftName" />
      </el-form-item>
      <el-form-item  label="是否启用">

        <el-radio-group v-model="addCraft.craftStatus">
          <el-radio value="0" size="large">是</el-radio>
          <el-radio value="1" size="large">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item  label="说明">
        <el-input type="textarea" v-model="addCraft.craftExplain" />
      </el-form-item>
      <el-form-item  label="备注">
        <el-input type="textarea" v-model="addCraft.craftRemark" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addSubmit">提交</el-button>
        <el-button type="info" @click="cancelProcess">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>


  <!-- 修改工艺流程弹窗 -->
  <el-dialog
      v-model="craftModifyWinDialogVisible"
      title="修改工艺路线"
      width="800"
  >
    <el-form v-model="editCraft" label-width="auto">
      <el-form-item  label="编号">
        <el-input v-model="editCraft.craftId" />
        <el-switch v-model="value1"  active-text="自动生产" :change="getRandom()"/>
      </el-form-item>
      <el-form-item  label="名称">
        <el-input  v-model="editCraft.craftName" />
      </el-form-item>
      <el-form-item  label="是否启用">

        <el-radio-group v-model="editCraft.craftStatus">
          <el-radio value="0" size="large">否</el-radio>
          <el-radio value="1" size="large">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item  label="工序详情">
        <el-input type="textarea" v-model="editCraft.craftExplain" />
      </el-form-item>
      <el-form-item  label="工序备注">
        <el-input type="textarea" v-model="editCraft.craftRemark" />
      </el-form-item>



    </el-form>
    <el-tabs
        v-model="activeName"
        type="card"
        class="demo-tabs"
        @tab-click="handleClick"
    >
      <el-tab-pane label="组成工序" name="first">
        <!-- 工具栏 -->
        <el-row style="margin-top: 10px">
          <el-col :span="24">
            <el-button type="primary" plain @click="toAddComposition"><el-icon><Plus /></el-icon>新增</el-button>
            <el-button type="success" plain @click="allPublish"><el-icon><EditPen /></el-icon>修改</el-button>
            <el-button type="danger" plain @click="deleteChooseOperate"><el-icon><Delete /></el-icon>删除</el-button>
          </el-col>
        </el-row>
        <!-- 组成工序数据表 -->
        <el-row style="margin-top: 10px">
          <el-col :span="24">
            <el-table
                :data="composition"
                border
                size="large"
                :stripe="true"
                ref="compositionTable"
            >
              <el-table-column type="selection" width="55" />

              <el-table-column fixed prop="compositionOrder" label="序号" />

              <el-table-column fixed prop="compositionId" label="工序编码" />
              <el-table-column fixed prop="compositionName" label="工序名称" />
              <el-table-column prop="compositionNext" label="下一道工序" />
              <el-table-column prop="compositionRelation" label="与下一道工序的关系" />
              <el-table-column prop="compositionStatus" label="关键工序">
                <template #default="scope">
                  <el-tag type="info" v-if="scope.row.compositionIsKey == 0">否</el-tag>
                  <el-tag type="primary" v-else-if="scope.row.compositionIsKey == 1">是</el-tag>
                </template>
              </el-table-column>

              <el-table-column prop="compositionStatus" label="质量确认">
                <template #default="scope">
                  <el-tag type="info" v-if="scope.row.compositionIsConfirm == 0">否</el-tag>
                  <el-tag type="primary" v-else-if="scope.row.compositionIsConfirm == 1">是</el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="compositionPrepareTime" label="准备时间"/>
              <el-table-column prop="compositionWaitTime" label="等待时间"/>
              <el-table-column  prop="compositionColor" label="甘特图显示颜色">
                <el-color-picker  prop="compositionColor" label="甘特图显示颜色"/>

              </el-table-column>

              <el-table-column fixed="right" label="操作" width="130">
                <template #default="scope">
                  <el-button-group>
                    <template v-if="scope.row.userId == curId">
                      <el-button size="small" type="primary" @click="toPrimaryComposition(scope.row.compositionRelId)">修改</el-button>
                      <el-popconfirm title="你确定要删除吗" @confirm="deleteOperate(scope.row.operateRelId)" >
                        <template #reference>
                          <el-button size="small" type="danger">删除</el-button>
                        </template>
                      </el-popconfirm>
                    </template>
                  </el-button-group>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <el-row style="margin: 0px 10px;padding: 0px 15px">
          <el-col :span="24">
            <el-pagination @change="changePage" background layout="prev, pager, next" :total="total"/>
          </el-col>
        </el-row>
        <el-row style="margin: 0px 10px;padding: 0px 15px ;">
          <el-col :span="16" >

          </el-col>
          <el-col :span="8" >
            <el-button type="primary" @click="editSubmit">提交</el-button>
            <el-button type="info" @click="canceledit">取消</el-button>
          </el-col>
        </el-row>
      </el-tab-pane>
      <el-tab-pane label="关联产品" name="second">
        <el-row style="margin-top: 10px">
          <el-col :span="25">
            <el-form :model="materialQuery" inline>
              <el-form-item label="产品物料编码">

                <el-input  v-model="materialQuery.materialId"/>
              </el-form-item>
              <el-form-item label="产品物料名称">
                <el-input v-model="materialQuery.materialName"/>
              </el-form-item>


              <el-form-item>
                <el-button @click="loadMaterial">搜索</el-button>
                <el-button @click="back">重置</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
        <!-- 工具栏 -->
        <el-row style="margin-top: 10px">
          <el-col :span="24">
            <el-button type="primary" plain @click="toAddMaterial"><el-icon><Plus /></el-icon>新增</el-button>
            <el-button type="success" plain @click="allPublish"><el-icon><EditPen /></el-icon>修改</el-button>
            <el-button type="warning" plain @click="push"><el-icon><EditPen /></el-icon>导出</el-button>
            <el-button type="danger" plain @click="deleteChooseOperate"><el-icon><Delete /></el-icon>删除</el-button>
          </el-col>
        </el-row>
        <!-- 关联产品数据表 -->
        <el-row style="margin-top: 10px">
          <el-col :span="24">
            <el-table
                :data="material"
                border
                size="large"
                :stripe="true"
                ref="compositionTable"
            >
              <el-table-column type="selection" width="55" />


              <el-table-column fixed prop="materialId" label="产品物料编码" />
              <el-table-column fixed prop="materialName" label="产品物料名称" />
              <el-table-column prop="materialSpecificationModel" label="规格型号" />
              <el-table-column prop="materialUtil" label="单位" />




              <el-table-column fixed="right" label="操作" width="130">
                <template #default="scope">
                  <el-button-group>
                    <template v-if="scope.row.userId == curId">
                      <el-button size="small" type="primary" @click="toPrimaryMaterial(scope.row.materialRelId)">修改</el-button>
                      <el-popconfirm title="你确定要删除吗" @confirm="deleteOperate(scope.row.operateRelId)" >
                        <template #reference>
                          <el-button size="small" type="danger">删除</el-button>
                        </template>
                      </el-popconfirm>
                    </template>
                  </el-button-group>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <el-row style="margin: 0px 10px;padding: 0px 15px">
          <el-col :span="24">
            <el-pagination @change="changePage" background layout="prev, pager, next" :total="total"/>
          </el-col>
        </el-row>
        <el-row style="margin: 0px 10px;padding: 0px 15px ;">
          <el-col :span="16" >

          </el-col>
          <el-col :span="8" >
            <el-button type="primary" @click="editSubmit">提交</el-button>
            <el-button type="info" @click="canceledit">取消</el-button>
          </el-col>
        </el-row>

        </el-tab-pane>
    </el-tabs>


  </el-dialog>

  <!-- 查看弹窗 -->
  <el-dialog
      v-model="craftDetailWinDialogVisible"
      title="查看工艺路线详情"
      width="600"
  >
    <el-form v-model="editCraft" label-width="auto">
      <el-form-item  label="编号">
        <el-input v-model="editCraft.craftId" />
        <el-switch v-model="value1"  active-text="自动生产" :change="getRandom()"/>
      </el-form-item>
      <el-form-item  label="名称">
        <el-input  v-model="editCraft.craftName" />
      </el-form-item>
      <el-form-item  label="是否启用">

        <el-radio-group v-model="editCraft.craftStatus">
          <el-radio value="0" size="large">否</el-radio>
          <el-radio value="1" size="large">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item  label="说明">
        <el-input type="textarea" v-model="editCraft.craftExplain" />
      </el-form-item>
      <el-form-item  label="备注">
        <el-input type="textarea" v-model="editCraft.craftRemark" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="cancelDetail">返回</el-button>
        <el-button type="info" @click="cancelDetail">取消</el-button>
      </el-form-item>
    </el-form>

    <span style="align-content: center">---------------------------操作步骤----------------------------------</span>
    <!-- 工具栏 -->
    <el-row style="margin-top: 10px">
      <el-col :span="24">
        <el-button type="primary" plain @click="toAddOperate"><el-icon><Plus /></el-icon>新增</el-button>
        <el-button type="success" plain @click="allPublish"><el-icon><EditPen /></el-icon>修改</el-button>
        <el-button type="danger" plain @click="allDelete"><el-icon><Delete /></el-icon>删除</el-button>
        <el-button type="warning" plain @click="exportOperate"><el-icon><Download /></el-icon>导出</el-button>
      </el-col>
    </el-row>
    <!-- 操作步骤数据表 -->
    <el-row style="margin-top: 10px">
      <el-col :span="24">
        <el-table
            :data="operate"
            border
            :stripe="true"
            ref="operationTable"
        >
          <el-table-column type="selection" width="55" />

          <el-table-column prop="operateId" label="顺序编号" >
            <template #default="scope">
              <el-link type="primary" >{{ scope.row.operateId }}</el-link>
            </template>
          </el-table-column>


          <el-table-column prop="operateExplain" label="步骤说明"/>

          <el-table-column prop="operateEquipmentAncillary" label="辅助设备"/>
          <el-table-column prop="operateMaterialsAncillary" label="辅助材料"/>
          <el-table-column prop="operateRemark" label="备注"/>
        </el-table>
      </el-col>
    </el-row>
    <el-row style="margin: 0px 10px;padding: 0px 15px">
      <el-col :span="24">
        <el-pagination @change="changePage" background layout="prev, pager, next" :total="total"/>
      </el-col>
    </el-row>
  </el-dialog>


  <!--新增工艺组成 -->
  <el-dialog
      v-model="compositionAddWinDialogVisible"
      title="添加工艺组成"
      width="500"
  >
    <el-form v-model="addComposition" label-width="auto">
      <el-form-item  label="序号">
        <el-input-number v-model="addComposition.compositionOrder" :min="1" :max="10" @change="handleChange" />
      </el-form-item>
      <el-form-item  label="工序">
        <el-select
            v-model="addComposition.compositionName"
            placeholder="Select"
            size="large"
            style="width: 240px"
        >
          <el-option
              v-for="item in processNames"
              :key="item.processName"
              :label="item.processName"
              :value="item.processName"
          />
        </el-select>
      </el-form-item>

      <el-form-item  label="与下一道工序的关系">
        <el-select
            v-model="addComposition.compositionRelation"
            placeholder="Select"
            size="large"
            style="width: 240px"
        >
          <el-option
              v-for="item in relations"
              :key="item.relation"
              :label="item.relation"
              :value="item.relation"
          />
        </el-select>
      </el-form-item>
      <el-form-item  label="甘特图显色">
        <el-color-picker v-model="addComposition.compositionColor" />
      </el-form-item>
      <el-form-item  label="是否为关键工序">
        <el-select
            v-model="addComposition.compositionIsKey"
            placeholder="Select"
            size="large"
            style="width: 240px"
        >
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item  label="是否需要质检确认">
        <el-select
            v-model="addComposition.compositionIsConfirm"
            placeholder="Select"
            size="large"
            style="width: 240px"
        >
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item  label="准备时间">
        <el-input-number v-model="addComposition.compositionPrepareTime" :min="1" :max="10" @change="handleChange" />
      </el-form-item>
      <el-form-item  label="等待时间">
        <el-input-number v-model="addComposition.compositionWaitTime" :min="1" :max="10" @change="handleChange" />
      </el-form-item>
      <el-form-item  label="备注">
        <el-input type="textarea" v-model="addComposition.compositionRemark" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addCompositionSubmit">确定</el-button>
        <el-button type="info" @click="cancelCompositionProcess">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

  <!--修改工序组成 -->
  <el-dialog
      v-model="compositionPrimaryWinDialogVisible"
      title="修改工艺组成"
      width="500"
  >
    <el-form v-model="primaryComposition" label-width="auto">
      <el-form-item  label="序号">
        <el-input-number v-model="primaryComposition.compositionOrder" :min="1" :max="10" @change="handleChange" />
      </el-form-item>
      <el-form-item  label="工序">
        <el-select
            v-model="primaryComposition.compositionName"
            placeholder="Select"
            size="large"
            style="width: 240px"
        >
          <el-option
              v-for="item in processNames"
              :key="item.processName"
              :label="item.processName"
              :value="item.processName"
          />
        </el-select>
      </el-form-item>

      <el-form-item  label="与下一道工序的关系">
        <el-select
            v-model="primaryComposition.compositionRelation"
            placeholder="Select"
            size="large"
            style="width: 240px"
        >
          <el-option
              v-for="item in relations"
              :key="item.relation"
              :label="item.relation"
              :value="item.relation"
          />
        </el-select>
      </el-form-item>
      <el-form-item  label="甘特图显色">
        <el-color-picker v-model="primaryComposition.compositionColor" />
      </el-form-item>
      <el-form-item  label="是否为关键工序">
        <el-select
            v-model="primaryComposition.compositionIsKey"
            placeholder="Select"
            size="large"
            style="width: 240px"
        >
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item  label="是否需要质检确认">
        <el-select
            v-model="primaryComposition.compositionIsConfirm"
            placeholder="Select"
            size="large"
            style="width: 240px"
        >
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item  label="准备时间">
        <el-input-number v-model="primaryComposition.compositionPrepareTime" :min="1" :max="10" @change="handleChange" />
      </el-form-item>
      <el-form-item  label="等待时间">
        <el-input-number v-model="primaryComposition.compositionWaitTime" :min="1" :max="10" @change="handleChange" />
      </el-form-item>
      <el-form-item  label="备注">
        <el-input type="textarea" v-model="primaryComposition.compositionRemark" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="primaryCompositionSubmit">确定</el-button>
        <el-button type="info" @click="cancelPrimaryComposition">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>


  <!--新增关联产品 -->
  <el-dialog
      v-model="materialAddWinDialogVisible"
      title="添加产品制程"
      width="500"
  >
    <el-form v-model="addMaterial" label-width="auto">

      <el-form-item  label="产品物料编码">
        <el-select
            v-model="addMaterial"
            placeholder="Select"
            size="large"
            style="width: 240px"
        >
          <el-option
              v-for="item in allMaterial"
              :key="item.materialId"
              :label="item.materialId"
              :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item  label="产品物料名称" >
        <el-input disabled  v-model="addMaterial.materialName" />
      </el-form-item>

      <el-form-item  label="单位" >
        <el-input disabled  v-model="addMaterial.materialUtil" />
      </el-form-item>

      <el-form-item  label="规格型号" >
        <el-input disabled  v-model="addMaterial.materialSpecificationModel" />
      </el-form-item>

      <el-form-item  label="备注" >
        <el-input disabled type="textarea" v-model="addMaterial.materialRemark" />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" @click="addMaterialSubmit">确定</el-button>
        <el-button type="info" @click="cancelAddMaterial">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

  <!--修改关联产品 -->
  <el-dialog
      v-model="materialPrimaryWinDialogVisible"
      title="修改产品制程"
      width="500"
  >
    <el-form v-model="primaryMaterial" label-width="auto">

      <el-form-item  label="产品物料编码">
        <el-select
            v-model="primaryMaterial"
            placeholder="Select"
            size="large"
            style="width: 240px"
        >
          <el-option
              v-for="item in allMaterial"
              :key="item.materialId"
              :label="item.materialId"
              :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item  label="产品物料名称" >
        <el-input disabled  v-model="primaryMaterial.materialName" />
      </el-form-item>

      <el-form-item  label="单位" >
        <el-input disabled  v-model="primaryMaterial.materialUtil" />
      </el-form-item>

      <el-form-item  label="规格型号" >
        <el-input disabled  v-model="primaryMaterial.materialSpecificationModel" />
      </el-form-item>

      <el-form-item  label="备注" >
        <el-input disabled type="textarea" v-model="primaryMaterial.materialRemark" />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" @click="primaryMaterialSubmit">确定</el-button>
        <el-button type="info" @click="cancelPrimaryMaterial">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

</template>
<script setup>
import {onMounted, ref} from "vue";
import produceRequest from "@/api/produce.js";
import {ElMessage, ElMessageBox} from "element-plus";
import craftRequest from "@/api/craft.js";
import craft from "@/api/craft.js";
import compositionRequest from "@/api/composition.js";
import materialRequest from "@/api/material.js";
let craftAddWinDialogVisible = ref(false);
let compositionPrimaryWinDialogVisible = ref(false);
let materialAddWinDialogVisible = ref(false);
let materialPrimaryWinDialogVisible = ref(false)
let operationTable =ref([]);
let primaryMaterial = ref({})
let material = ref([])
let materialQuery = ref({})
let addComposition = ref({});
const activeName = ref('first')
//所有的material数据
let allMaterial = ref([])
let composition = ref([])
let curPage = ref(1);
// 总条数，默认0
let total = ref(0);
let addMaterial = ref({})
let compositionAddWinDialogVisible =ref(false);
let craftQuery = ref({});
//存放选择数据
let processTable = ref([]);
//新增公路线
let addCraft =ref({});
let processNames = ref([])
let craftList = ref([]);
let value1 = ref();
let editCraft = ref({})
let craftDetailWinDialogVisible =ref(false);
let craftModifyWinDialogVisible = ref(false);
let primaryComposition = ref({})
let flagId = ref({})
let options = ref([{label:"是",value:"1"},{label:"否",value:"0"}])
let relations = ref([{relation:"S-to-S"},{relation:"F-to-F"},{relation:"S-to-F"},{relation:"F-to-S"}])
function primaryMaterialSubmit(){
  console.log(primaryMaterial.value)
  primaryMaterial.value.flagId =flagId.value
  materialRequest.primaryMaterial(primaryMaterial.value,res=>{
    if (res.code=="200"){
      ElMessage({
        type:"success",
        message:"修改成功"
      })
      toPrimaryCraft(editCraft.value.craftRelId);
      materialPrimaryWinDialogVisible.value = false;

    }else {
      ElMessage({
        type:"error",
        message:"添加失败"
      })

    }
  })
}

//打开修改关联产品界面
function toPrimaryMaterial(materialRelId){
  materialRequest.getPrimaryMaterial(materialRelId,editCraft.value.craftRelId,res=>{
    primaryMaterial.value = res.data;
    flagId.value = primaryMaterial.value.flagId;
    materialPrimaryWinDialogVisible.value = true;
  })
}
//确认增加产品制程
function addMaterialSubmit(){
  materialRequest.addMaterial(addMaterial.value.materialRelId,editCraft.value.craftRelId,res=>{
    console.log(res)
    if (res.code=="1"){
      ElMessage({
        type:"success",
        message:"添加成功"
      })
      toPrimaryCraft(editCraft.value.craftRelId);


    }else {
      ElMessage({
        type:"error",
        message:"添加失败"
      })

    }
  })
}
function cancelAddMaterial(){
  materialAddWinDialogVisible.value = false;
  addMaterial = {}
}
function toAddMaterial(){
  materialAddWinDialogVisible.value = true;
}


//导出工序步骤
function exportOperate(){
  produceRequest.exportOperate(editProcess.value.processRelId,1,10);
}
//批量删除工序步骤
function deleteChooseOperate(){
  let selectedRows = operationTable.value.getSelectionRows();
  console.log(selectedRows)

  ElMessageBox.confirm(
      '您确定要将选中的项目删除吗？',
      '提示',
      {
        confirmButtonText: '是的',
        cancelButtonText: '点错了',
        type: 'warning',
      }
  ).then(()=>{
    produceRequest.deleteChooseOperate(selectedRows,res=>{
      if (res.code=="200"){
        ElMessage({
          type:"success",
          message:"成功删除"
        })

        toPrimaryProcess(editProcess.value.processRelId);
      }else {
        ElMessage({
          type:"error",
          message:"数据错误"
        })

      }

    })
  })

}
//删除单个工序步骤
function deleteOperate(id){
  produceRequest.deleteOneOperate(id,res=>{
    if (res.code=="200"){
      ElMessage({
        type:"success",
        message:"删除成功"
      })
      toPrimaryProcess(editProcess.value.processRelId);


    }else {
      ElMessage({
        type:"error",
        message:"删除失败"
      })

    }

  })
}
//确认修改
function primaryCompositionSubmit(){
  console.log(primaryComposition.value)
  compositionRequest.primaryComposition(primaryComposition.value,res=>{
    if (res.code=="200"){
      ElMessage({
        type:"success",
        message:"修改成功"
      })
      toPrimaryCraft(primaryComposition.value.compositionCraftId);
      compositionPrimaryWinDialogVisible.value = false;

    }else {
      ElMessage({
        type:"error",
        message:"数据错误"
      })

    }
  })
}

//取消修改工序步骤
function cancelCompositionPrimary(){
  operatePrimaryWinDialogVisible.value = false;


}
//工艺组成修改的按键
function toPrimaryComposition(compositionRelId){
  compositionRequest.getPrimaryComposition(compositionRelId,res=>{
    primaryComposition.value = res.data;
    compositionPrimaryWinDialogVisible.value = true;

  })

}
//取消新增工艺组成
function cancelAddComposition(){

  compositionAddWinDialogVisible.value = false;
}

//确认新增工艺组成
function addCompositionSubmit(){
  addComposition.value.compositionCraftId = editCraft.value.craftRelId;
  console.log(addComposition.value);
  compositionRequest.addComposition(addComposition.value,res=>{
    if (res.code=="200"){
      ElMessage({
        type:"success",
        message:"添加成功"
      })
      toPrimaryCraft(addComposition.value.compositionCraftId);
      compositionAddWinDialogVisible.value = false;

    }else {
      ElMessage({
        type:"error",
        message:"数据错误"
      })

    }
  })
}


//新增工艺组成
function toAddComposition(){

  compositionAddWinDialogVisible.value=true;

}

//存放操作步骤
let operate = ref([])
//批量删除
function allDelete(){
  let selectedRows = processTable.value.getSelectionRows();

  ElMessageBox.confirm(
      '您确定要将选中的项目删除吗？',
      '提示',
      {
        confirmButtonText: '是的',
        cancelButtonText: '点错了',
        type: 'warning',
      }
  ).then(()=>{
    produceRequest.deleteAllProcess(selectedRows,res=>{
      if (res.code=="200"){
        ElMessage({
          type:"success",
          message:"成功删除"
        })

        loadProcesses();
      }else {
        ElMessage({
          type:"error",
          message:"数据错误"
        })

      }

    })
  })

}
//删除确认
function deleteCraft(relId){
  craftRequest.deleteCraft(relId,res=>{
    if (res.code=="200"){
      ElMessage({
        type:"success",
        message:"成功删除"
      })

      loadCraft();
    }else {
      ElMessage({
        type:"error",
        message:"数据错误"
      })

    }
  })

}
function cancelDetail(){
  craftDetailWinDialogVisible.value=false;
  editCraft.value="";
}
function toDetail(id){

  craftRequest.getPrimaryCraft(id,res=>{
    editCraft.value = res.data;
    console.log(editCraft.value)
    craftDetailWinDialogVisible.value=true;
  })


}
//确认修改
function editSubmit(){
  console.log(editCraft.value)
 craftRequest.primaryCraft(editCraft.value,res=>{
    if (res.code=="200"){
      ElMessage({
        type:"success",
        message:"成功修改"
      })
      craftModifyWinDialogVisible.value = false;
      loadCraft();
    }else {
      ElMessage({
        type:"error",
        message:"数据错误"
      })

    }

  })
craftModifyWinDialogVisible.value = false;

}

//取消修改
function canceledit(){
  craftModifyWinDialogVisible.value = false;
  editCraft.value = "";
}
//修改按钮
function toPrimaryCraft(craftRelId){

  editCraft.value = "";
  craftRequest.getPrimaryCraft(craftRelId,res=>{
    editCraft.value = res.data;
  })

  compositionRequest.getCraft(craftRelId,1,10,res=>{
    console.log(res.data)
    composition.value = res.data.list;
  })

  materialRequest.getMaterial(materialQuery.value,craftRelId,1,10,res=>{
    console.log(res.data)
    material.value = res.data.list;
  })


  craftModifyWinDialogVisible.value = true;
}
function getRandom(){
  if (value1.value==true){
    produceRequest.getRandom(res=>{
      addCraft.value.craftId = res.data;
    })
  }
}

function addSubmit(){

  console.log(addCraft)
 craftRequest.addCraft(addCraft.value,res=>{
    console.log(res)
    if (res.code=="200"){
      ElMessage({
        type:"success",
        message:"成功增加"
      })
      craftAddWinDialogVisible.value = false;
      loadCraft();
    }else {
      ElMessage({
        type:"error",
        message:"数据错误"
      })

    }
  })
}
function cancelProcess(){
  craftAddWinDialogVisible.value = false;
  addCraft.value = {}
}
function toAdd(){
  craftAddWinDialogVisible.value = true;
}
function changePage(value) {
  curPage.value = value;
  loadCraft();
}
function loadCraft(){

  craftRequest.getCraft(craftQuery.value,curPage.value,10,res=>{

    total.value = res.data.total;
    // 将查询的列表赋值给projects
    craftList.value = res.data.list;
    console.log(res)
  })
}
onMounted(()=>{
  compositionRequest.getProcessName(res=>{

    processNames.value = res.data;
    console.log(processNames)
  })
  materialRequest.getAllMaterial(res=>{
    allMaterial.value = res.data;
  })
  loadCraft();
})
</script>

<style scoped>

</style>
